package com.smartapplication.smartringer;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.widget.Toast;

public class SmartRinger extends PreferenceActivity {

	public static int minVol; // default = 3
	public static int maxVol; // default = 7
	public static int micSens; // default = 0

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.preferences);
		readPrefVal();

		// Call Toast notification with displaying Current noise level value
		Preference savePref = (Preference) findPreference("testPref");
		savePref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
			public boolean onPreferenceClick(Preference preference) {
				readPrefVal();
				int currentNoise = toastDB() + micSens;
				Toast.makeText(
						getBaseContext(),
						getString(R.string.Current_noise_level_is)
								+ currentNoise + getString(R.string.dB_string),
						Toast.LENGTH_LONG).show();
				return true;
			}
		});

		// Call Alert Dialog with about text
		Preference aboutPref = (Preference) findPreference("aboutPref");
		aboutPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
			public boolean onPreferenceClick(Preference preference) {
				AlertDialog alertDialog = new AlertDialog.Builder(
						SmartRinger.this).create();
				alertDialog.setTitle(R.string.aboutDialog_Title);
				alertDialog.setMessage(getString(R.string.about_txt));
				alertDialog.setButton(getString(R.string.OK_string),
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int which) {
							}
						});
				alertDialog.show();
				return true;
			}
		});

		// Here are executed events carried out by clicking checkbox
		final CheckBoxPreference checkboxPref = (CheckBoxPreference) getPreferenceManager()
				.findPreference("checkboxEnableService");
		checkboxPref
				.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
					public boolean onPreferenceChange(Preference preference,
							Object newValue) {
						if (newValue.toString().equals("true")) {
							startService(new Intent(getApplicationContext(),
									SmartService.class));
						} else {
							stopService(new Intent(getApplicationContext(),
									SmartService.class));
						}
						return true;
					}
				});

		// Here are executed events carried out by choosing ListPreference
		// minVolPref
		final ListPreference minVolPref = (ListPreference) findPreference("minVolPref");
		minVolPref
				.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
					public boolean onPreferenceChange(Preference preference,
							Object newValue) {
						readPrefVal();
						minVol = Integer.parseInt(newValue.toString());
						if (minVol >= maxVol) {
							Toast.makeText(SmartRinger.this,
									R.string.Error_min, Toast.LENGTH_SHORT)
									.show();
							return false;
						}

						Toast.makeText(
								SmartRinger.this,
								getString(R.string.Minimal_volume_set_to)
										+ newValue.toString(),
								Toast.LENGTH_SHORT).show();
						return true;
					}
				});

		// Here are executed events carried out by choosing ListPreference
		// maxxVolPref
		final ListPreference maxxVolPref = (ListPreference) findPreference("maxxVolPref");
		maxxVolPref
				.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
					public boolean onPreferenceChange(Preference preference,
							Object newValue) {
						readPrefVal();
						maxVol = Integer.parseInt(newValue.toString());
						if (maxVol <= minVol) {
							Toast.makeText(SmartRinger.this,
									R.string.Error_max, Toast.LENGTH_SHORT)
									.show();
							return false;
						}
						Toast.makeText(
								SmartRinger.this,
								getString(R.string.Maximal_volume_set_to)
										+ newValue.toString(),
								Toast.LENGTH_SHORT).show();
						return true;
					}
				});

		// Here are executed events carried out by choosing ListPreference
		// micSensPref
		final ListPreference micSensPref = (ListPreference) findPreference("micSensPref");
		micSensPref
				.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
					public boolean onPreferenceChange(Preference preference,
							Object newValue) {
						Toast.makeText(
								SmartRinger.this,
								getString(R.string.Mic_sensitivity_set_to)
										+ newValue.toString()
										+ getString(R.string.dB_string),
								Toast.LENGTH_SHORT).show();
						return true;
					}
				});

	}

	// This is necessary to obtain the value of noise Level
	public static IncomingCallReceiver checkN = null;
	private int Noize;

	public int toastDB() {
		checkN = new IncomingCallReceiver();
		Noize = checkN.checkCurrentNoise();
		return Noize;
	}

	// This is performed if need to update the values ​​of variables
	public void readPrefVal() {
		SharedPreferences sharedPrefs = PreferenceManager
				.getDefaultSharedPreferences(this);
		minVol = Integer.parseInt(sharedPrefs.getString("minVolPref", "3"));
		maxVol = Integer.parseInt(sharedPrefs.getString("maxxVolPref", "7"));
		micSens = Integer.parseInt(sharedPrefs.getString("micSensPref", "0"));
	}

}